package com.pignest.hotel.system;

import java.util.Date;
import java.util.List;

import org.zkoss.zul.Label;
import org.zkoss.zul.Textbox;

import com.pignest.framework.util.Utils;
import com.pignest.hotel.beans.HotelBill;
import com.pignest.hotel.beans.Living;
import com.pignest.hotel.beans.Room;
import com.pignest.hotel.beans.TbCloseAccounts;
import com.pignest.hotel.beans.TbLiveGoodsBorrow;
import com.pignest.hotel.beans.User;
import com.pignest.hotel.definition.BillType;
import com.pignest.hotel.definition.LivingState;
import com.pignest.hotel.definition.OpenRoomType;
import com.pignest.hotel.spring.Services;
import com.pignest.hotel.util.DateUtil;
import com.pignest.hotel.util.UIUtils;

public class InComeUtil {
	
	public static void countInComeAll(List<Living> ls,
			     Label lStorage,Label lUsed,Label lPayed,Label lBalance ,
			     Textbox tAdd,int addDay) {
		int iStorage=Services.getIncomeService().countAllStorages(ls, false);//总存款
		int iUsed=Services.getIncomeService().countAllConsumsUsed(ls, true);//已消费
		int iPayed =Services.getIncomeService().countAllConsumsPayed(ls, true);//已付
		int iAddpay=0;
	    for(Living l:ls){
	        int aprice =l.getAdjustPrice();
			iAddpay=iAddpay+aprice*addDay/2;
			}
			tAdd.setValue(Utils.toPrice(iAddpay));
		
		lStorage.setValue(Utils.toPrice(iStorage));
		iUsed=iUsed+iAddpay;
		int iLast =iStorage-iUsed+iPayed;

		lUsed.setValue(Utils.toPrice(iUsed));
		lBalance.setValue(Utils.toPrice(iLast));
		
		lPayed.setValue(Utils.toPrice(iPayed));
		
	}
	public static void countInComeSingle(Living l,Label lStorage,Label lUsed,Label lPayed,Label lBalance ,
			     Textbox tAdd,int addDay){
		int sStorage =Services.getIncomeService().countAllStroages(l,false);
		int sPayed =Services.getIncomeService().countAllConsums(l, true, true);
		int sUsed =Services.getIncomeService().countAllConsumsUsed(l, true,null);;
		
		int sPrice =l.getAdjustPrice();
		int sAddPay=addDay*sPrice/2;
		sUsed=sUsed+sAddPay;
		int sLast=sStorage-sUsed+sPayed;
		lBalance.setValue(Utils.toPrice(sLast));
		lStorage.setValue(Utils.toPrice(sStorage));
		lUsed.setValue(Utils.toPrice(sUsed));
		tAdd.setValue(Utils.toPrice(sAddPay));
		lPayed.setValue(Utils.toPrice(sPayed));
	}
	public static void countInComeNoAdd(List<Living> ls ,Label lStorage,Label lUsed,Label lPayed,Label lBalance){
		int iStorage=Services.getIncomeService().countAllStorages(ls, false);//总存款
		int iUsed=Services.getIncomeService().countAllConsumsUsed(ls, true);//已消费
		
		int iPayed=0;//已支付

		iPayed =Services.getIncomeService().countAllConsumsPayed(ls, true);//已付
		int iBalance=iStorage-iUsed+iPayed;//余额
		lStorage.setValue(Utils.toPrice(iStorage));
		lUsed.setValue(Utils.toPrice(iUsed));
		lBalance.setValue(Utils.toPrice(iBalance));
		lPayed.setValue(Utils.toPrice(iPayed));
	}
	public static void countInComeHours(List<Living> ls ,Label lStorage,Label lUsed,Label lPayed,Label lBalance,int times,Textbox tAccount){
		int iStorage=Services.getIncomeService().countAllStorages(ls, false);//总存款
		int iUsed=Services.getIncomeService().countAllConsumsUsed(ls, true);//已消费
		int iAccount=0;
	    for(Living l:ls){
	        int aprice =l.getAdjustPrice();
	        iAccount=iAccount+aprice*times;
			}
	    tAccount.setValue(Utils.toPrice(iAccount));
	    iUsed =iUsed+iAccount;
		int iPayed=0;//已支付

		iPayed =Services.getIncomeService().countAllConsumsPayed(ls, true);//已付
		int iBalance=iStorage-iUsed+iPayed;//余额
		lStorage.setValue(Utils.toPrice(iStorage));
		lUsed.setValue(Utils.toPrice(iUsed));
		lBalance.setValue(Utils.toPrice(iBalance));
		lPayed.setValue(Utils.toPrice(iPayed));
	}
	public static void countInComeHours(Living l,Label lStorage,Label lUsed,Label lPayed,Label lBalance,int times,Label tAccount){
		int iStorage =Services.getIncomeService().countAllStroages(l, false);//总存
		int iAccount=0;
		iAccount =times*l.getAdjustPrice();
		int iUsed=Services.getIncomeService().countAllConsumsUsed(l, true,null);//总消费
		iUsed =iUsed+iAccount;
		int iPaye =Services.getIncomeService().countPayed(l,true);//已支付的
		int sLast=iStorage-iUsed+iPaye;//余额
		lStorage.setValue(Utils.toPrice(iStorage));
		lUsed.setValue(Utils.toPrice(iUsed));
		lBalance.setValue(Utils.toPrice(sLast));
		lPayed.setValue(Utils.toPrice(iPaye));
		tAccount.setValue(Utils.toPrice(iAccount));
		
	}
	public static void countInComeNoAdd(Living l,Label lStorage,Label lUsed,Label lPayed,Label lBalance){
		int iStorage =Services.getIncomeService().countAllStroages(l, false);//总存
		int iUsed=Services.getIncomeService().countAllConsumsUsed(l, true,null);//总消费
		int iPaye =Services.getIncomeService().countPayed(l,true);//已支付的
		int sLast=iStorage-iUsed+iPaye;//余额
		lStorage.setValue(Utils.toPrice(iStorage));
		lUsed.setValue(Utils.toPrice(iUsed));
		lBalance.setValue(Utils.toPrice(sLast));
		lPayed.setValue(Utils.toPrice(iPaye));
		
	}
	/**计算入住时的总存款，已使用、已支付、余额
	 * 总存款包括续存款
	 * 已使用 = 所有已经消费的包括已结账的+未结账的消费
	 * 已支付 = 已结账的消费
	 * 余额      =总存款-已使用
	 * **/
	public static void updateLiving(String livingId){
		System.out.println("**********"+livingId);
		Living l = Services.getReceptionService().get(Living.class, livingId);
		int iStorage =Services.getIncomeService().countAllStroages(l, false);//总存
		int iUsed=Services.getIncomeService().countAllConsumsUsed(l, true,null);//总消费
		int iPaye =Services.getIncomeService().countPayed(l,true);//已支付的
		int sLast=iStorage-iUsed+iPaye;//余额
		l.setStorage(iStorage);
		l.setUsed(iUsed);
		l.setPayed(iPaye);
		l.setAvailable(sLast);
		Services.getIncomeService().update(l);
		//return l;
	}
    public static boolean isAllSameLivingState(List<Living> ls,String state){
    	if(ls==null)
    		return false;
    	int aCount =ls.size();
    	int i=0;
    	for(Living l:ls){
    		if(state.equals(l.getLiveState())){
    			i=i+1;
    		}
    	}
    	if(aCount==i)
    		return true;
    	else
    		return false;
    }
    public static boolean isNoIncludeCheckOutState(List<Living> ls)
    {
    	if(ls==null)
    		return false;
    	int aCount =ls.size();
    	int i=0;
    	for(Living l:ls){
    		if(!LivingState.CHECKOUT.name().equals(l.getLiveState())){
    			i=i+1;
    		}
    	}
    	if(aCount==i)
    		return true;
    	else
    		return false;
    }
    public static boolean isNoSameByOpenRoomState(List<Living> ls,String openState){
    	if(ls==null)
    		return false;
    	int aCount =ls.size();
    	int i=0;
    	for(Living l:ls){
    		if(openState.equals(l.getOpenRoomType())){
    			i=i+1;
    		}
    	}
    	if(aCount==i)
    		return true;
    	else
    		return false;
    }
    /**是否有未归还的
	 * */
	public static boolean isNoBorrow(List<Living> list){
		String r=null;
		List<TbLiveGoodsBorrow> noList =Services.getIncomeService().isNoBorrow(list);
		if(noList==null){
			return true;
		}
		else{
			for(TbLiveGoodsBorrow b:noList){
				r=r+b.getLiving().getRoom().getName()+":"+b.getTbGoods().getName()+",";
			}
			UIUtils.alert(r);
			return false;
		}
	}
	public static String getAutoLivingReMark(List<Living> list,boolean isShowMoney){
		String r="";
		int i=0;
		for(Living l:list){
			int b=Services.getIncomeService().querySingleConsum(l.getId(), true);
			
				if(isShowMoney){
				    r=r+l.getRoom().getName()+"["+l.getRoom().getType().getName()+"]"+"开房方式:"+OpenRoomType.valueOf(l.getOpenRoomType()).getDisplayName()+":消费:"+Utils.toPrice(b)+"\n";
				}else
					r=r+l.getRoom().getName()+"["+l.getRoom().getType().getName()+"]"+"开房方式:"+OpenRoomType.valueOf(l.getOpenRoomType()).getDisplayName()+"\n";;
			}
			i++;
		return r;
	}
	/**
	 * 系统默认情况下应收金
	 * 天房
	 * **/
	public static int sysInComeDays(List<Living> ls)
	{
		int iUsed=Services.getIncomeService().countAllConsumsUsed(ls, true);//已消费
		int iAddpay=0;
	    for(Living l:ls){
	        int aprice =l.getAdjustPrice();
	        int add=Logic.getLivingAdditionalTime(l.getLiveTime(),new Date(),true);
			iAddpay=iAddpay+aprice*add/2;
		}
	    iUsed=iUsed+iAddpay;
	    return iUsed;
	}
	public static int sysInComeHours(List<Living> ls)
	{
		int iUsed=Services.getIncomeService().countAllConsumsUsed(ls, true);//已消费
		int iAccount=0;
		 for(Living l:ls){
		        int aprice =l.getAdjustPrice();
		        int times =Logic.countUnionHours(new Date(),l.getLiveTime());
		        iAccount=iAccount+aprice*times;
				}
	    iUsed=iUsed+iAccount;
	    return iUsed;
	}
	public static int sysInComeHalfDay(List<Living> ls)
	{
		int iUsed=Services.getIncomeService().countAllConsumsUsed(ls, true);//已消费
		int iAccount=0;
		 for(Living l:ls){
		        int aprice =l.getAdjustPrice();
		        iAccount=iAccount+aprice;
				}
	    iUsed=iUsed+iAccount;
	    return iUsed;
	}
	
	public static void jjj (){
		Services.getIncomeService().executeInTransaction(new Runnable() {
			@Override
			public void run() {
				//xx
			}
		});
	}

}
